// pages/account/center/payPassword/payPassword.js
const http = require('../../../../utils/http.js')
const Base64 = require('../../../../utils/base64.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: http.imgUrl,
    navBar: {
      title: '设置支付密码',
      btn: 'goBack'
    },
    canClick: true,
    codeMsg: '发送验证码',
    phone: "",
    code: "",
    codeMsg: "获取验证码",
    disabled: false,
    disabledone: true,
    inputArr:[],
    payPayPwdInput:[],
    setup:'',
    showPayPwdInput: false,  //是否展示密码输入层
    pwdVal: '',  //输入的密码
    payFocus: true, //文本框焦点
    cid:'',
    id_str:'',
    aid:''
  },
    goBack() {
      http.goBack()
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var yes = options.lucky
    var cid = options.id
    var id_str = options.id_str
    var aid = options.aid
    // console.log(aid)
    // console.log(yes)
    if(!options){
      this.setData({
        setup: yes,
        cid: cid,
        id_str: id_str,
        aid: aid
      })
    }else{
   
    }
   
    const token = wx.getStorageSync('token')
  },
    
  inputTxt(e) {
    var inputArr = this.data.inputArr
    var temp = http.inputArr(inputArr,e)
    this.setData({
      inputArr: temp
    })
  },

    getCode:function(){
      var url = 'User_getMessage'
     var tel= this.data.inputArr[0]
      console.log(Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'))
     console.log(tel)
      var codeMsg = this.data.codeMsg
      if (!http.ifEmpty(tel)) {
        wx.showToast({
          title: '请填写手机号',
          icon: 'none',
          duration: 3000
        })
        return false
      }

      if (!http.rexFn(tel)[0]) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none',
          duration: 3000
        })
        return false
      }
      
      if (codeMsg == '重新获取' || codeMsg == "获取验证码") {
        
        //验证码请求
        http._post(
          url,
          { mobile: Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'), type: 4 },
          res => {
            let data = res.data
            console.log(res)
            if (data.re === '1') {
              let time = 60;
              let clearTime = setInterval(() => {
                if (time != 0) {
                  time--
                  this.setData({
                    codeMsg: time + '秒'
                  })
                } else {
                  clearInterval(clearTime);
                  this.setData({
                    codeMsg: '重新获取'

                  })
                }
              }, 1000)
              wx.showToast({
                title: '短信发送成功',
                icon: 'none',
                duration: 3000
              })
              this.setData({
                disabled: true,
                disabledone: false,
              })
            } else {
              wx.showToast({
                title: data.info,
                icon: 'none',
                duration: 3000
              })
            }
          },
          err => {
            console.log(err)
          }
        )//验证码请求


      }
     },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
    bindPhone: function (e) {       //获取手机号码
      // console.log(e)
      var phone = e.detail.value;
      console.log(phone)
      this.setData({
        phone: phone
      })
    },
    bindCode: function (e) {        //获取手机验证码
      var code = e.detail.value;
      console.log(code)
      this.setData({
        code: code
      })
    },  

  onGotUserInfo:function(){
    var setup = this.data.setup
    var cid = this.data.cid
    var id_str = this.data.id_str
    var aid = this.data.aid
    var url = 'User_forgotPassword'
    var tel = this.data.inputArr[0] //获取电话号码
    console.log(Base64.CusBASE64.encoder('UNO' + tel + 'HACHA')) //mobile  
    var title = this.data.pwdVal
    // console.log(title)    //支付密码password
    var password = Base64.CusBASE64.encoder('UNO' + title + 'HACHA')
    var code = this.data.inputArr[1]
    // console.log(code)     //支付验证码
    http._post(
      url,
      { mobile: Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'),code:code,password:password,type:2},
      res=>{
        let data = res.data
        console.log(data)
        if(data.re==='1'){
          wx.showToast({
            title: '密码设置成功',
          })
          if(setup==='1'){
            var back=[]
            back["cid"] = cid
            back["id_str"] = id_str
            back["aid"] = aid 
            app.globalData.back = back
            wx.navigateTo({
              url: '/pages/course/pay/pay',
            })
          }
          if (setup === '2') {
            var back = []
            back["cid"] = cid
            back["id_str"] = id_str
            back["aid"] = aid
            app.globalData.back = back
            wx.navigateTo({
              url: '/pages/course/soiDetail/soiDetailPay/soiDetailPay',
            })
          }
        }      
      },
      err => {
        console.log(err)
      }
    )
  },
  // * 显示支付密码输入层
  //  */
  showInputLayer: function () {
      var title = this.data.pwdVal
      var code = this.data.inputArr[1]
      var tel = this.data.inputArr[0]
      if(!tel){
        wx.showToast({
          title: '请输入手机号',
          icon:"none",
          duration:3000
        })
        return false
      }
      if (tel<11) {
        wx.showToast({
          title: '请输入正确手机号',
          icon: "none",
          duration: 3000
        })
        return false
      }
      if (code<6) {
        wx.showToast({
          title: '验证码不正确',
          icon: "none",
          duration: 3000
        })
        return false
      }
      if (!code) {
        wx.showToast({
          title: '请获取验证码',
          icon: "none",
          duration: 3000
        })
        return false
      }
      if(code>5){
        this.setData({ showPayPwdInput: true, payFocus: true });
      }
         
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function () {
    var val = this.data.pwdVal;
    // console.log(val)   //输入的支付密码
    if(!val){
      this.setData({
        showPayPwdInput: false, payFocus: false, pwdVal: val
      })
    }else{
      this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: val }, function () {
         
      });
    }  
  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    this.setData({ pwdVal: e.detail.value });

    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
      this.onGotUserInfo();
    }
    
  },


  onReady: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  
  onShow: function () {

  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function checkMobile(inputArr) {
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
  if (!myreg.test(inputArr)) {
      return false;
    } else {
      return true;
    }
  }